<?php
	$upload_dir = "/home/projetos/anaweb/anaweb/upload/upload_data";
	$upload_metadata = "/home/projetos/anaweb/anaweb/upload/upload_metadata";
	$archive = $upload_dir . "/". $_GET['arq'];
	if (!isset($_GET['prof']) || !isset($_GET['arq']))
	{
		die("Erro: o link digitado esta incorreto.");
	}
	
	$metadata = $upload_metadata . "/" . $_GET['prof'] . ".xml";
	if (!file_exists($metadata))
	{
		die("Erro: professor nao encontrado, tente novamente");
	}
	$xml = new DOMDocument("1.0","utf-8");
	if (!$xml->load($metadata))
	{
		die("Erro no arquivo de metadados, tente novamente");
	}
	if (!file_exists($archive))
	{
		die("Arquivo nao encontrado");
	}
	$arquivos = $xml->getelementsbytagname("files");
	foreach ($arquivos as $arquivo)
	{
		$nome = $arquivo->getelementsbytagname("filename");
		$original = $nome->item(0)->getelementsbytagname("original")->item(0)->nodeValue;
		$stored = $nome->item(0)->getelementsbytagname("stored")->item(0)->nodeValue;
		if ($stored == $_GET['arq'])
		{
			header("Content-Type: application/octet-stream");
			header("Content-Length: " . filesize($archive));
			header("Content-Disposition: attachment; filename=\"".$original."\"");
			readfile($archive);
			exit();
		}
	}
?>
